package org.springblade.third.provider;

import org.springblade.third.dto.ThirdRequest;
import org.springblade.third.dto.ThirdResponse;
import org.springblade.third.spi.ChannelProvider;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

@Component
public class HttpEchoProvider implements ChannelProvider {

    private final RestTemplate restTemplate = new RestTemplate();

    @Override
    public boolean supports(String channelCode, String productCode) {
        return "HTTP".equalsIgnoreCase(channelCode) && "ECHO".equalsIgnoreCase(productCode);
    }

    @Override
    public ThirdResponse<Object> execute(ThirdRequest request) {
        Map<String, Object> p = request.getParameters();
        String url = p == null ? null : String.valueOf(p.getOrDefault("url", "https://httpbin.org/anything"));
        ResponseEntity<String> resp = restTemplate.exchange(url, HttpMethod.GET, null, String.class);
        return ThirdResponse.ok(resp.getBody());
    }
}



